package com.kotlinvben.bean

data class Result<T>(
    val code: Int,
    val data: T? = null,
    val message: String? = null,
    val error: String? = null
) {
    companion object {
        fun <T> success(data: T? = null, message: String? = "success"): Result<T> {
            return Result(
                code = 0,
                data = data,
                message = message
            )
        }

        fun <T> error(code: Int, message: String? = null, error: String? = null): Result<T> {
            return Result(
                code = code,
                message = message,
                error = error
            )
        }

        fun <T> error(message: String? = null, error: String? = null): Result<T> {
            return error(500, message, error)
        }

        fun <T> unauthorized(message: String? = "未授权访问"): Result<T> {
            return error(401, message)
        }

        fun <T> forbidden(message: String? = "禁止访问"): Result<T> {
            return error(403, message)
        }

        fun <T> notFound(message: String? = "资源不存在"): Result<T> {
            return error(404, message)
        }

        fun <T> badRequest(message: String? = "请求参数错误"): Result<T> {
            return error(400, message)
        }
    }
} 